package net.quanter.sketch;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/***
 *
 * 2020-07-14
 * @author harley-dog
 *
 */
public final class ImageUtil {

    public static void getImage(String outFile,Map<String,String> variableMap) throws Exception {

        InputStream inputStreamSvg = ImageUtil.class.getClassLoader().getResourceAsStream("template.svg");
        InputStream inputStreamJpg = ImageUtil.class.getClassLoader().getResourceAsStream("template.jpg");
        OutputStream outputStream = new FileOutputStream(outFile);
        List<Point> pointList = SvgParser.parse(inputStreamSvg);
        Map<String,Point> pointMap = pointList.stream().collect(Collectors.toMap(Point::getId, point -> point));
        BufferedImage bufferedImage = ImageIO.read(inputStreamJpg);
        Graphics2D graphics2D = bufferedImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        graphics2D.setColor(Color.BLACK);
        variableMap.forEach((x,y)->{
            if(pointMap.containsKey(x)){
                Point point = pointMap.get(x);
                graphics2D.drawString(y,point.getX(),point.getY());
            }
        });
        ImageIO.write(bufferedImage, "JPG", outputStream);
        graphics2D.dispose();
    }
}
